package com.ruoyi.waterDistribution.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.business.waterDistribution.WiCropB;
import com.ruoyi.common.core.domain.business.waterDistribution.WiCropY;
import com.ruoyi.common.core.utils.calendar.CalendarUtils;
import com.ruoyi.common.core.utils.zeroFill.ZeroFillUtils;
import com.ruoyi.waterDistribution.mapper.WiCropBMapper;
import com.ruoyi.waterDistribution.vo.WiCropYVo;
import com.ruoyi.waterDistribution.mapper.WiCropYMapper;
import com.ruoyi.waterDistribution.service.IWiCropYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 年度种植作物Service业务层处理
 *
 * @author ps
 * @date 2023-02-16
 */
@Service
public class WiCropYServiceImpl implements IWiCropYService {
    @Autowired
    private WiCropYMapper wiCropYMapper;
    @Autowired
    private WiCropBMapper wiCropBMapper;

    /**
     * 查询年度种植作物
     *
     * @param id 年度种植作物主键
     * @return 年度种植作物
     */
    @Override
    public WiCropYVo selectWiCropYById(String id) {
        WiCropYVo wiCropYVo1 = wiCropYMapper.selectWiCropYById(id);
        if (wiCropYVo1 != null) {
            WiCropB wiCropB = wiCropBMapper.selectWiCropBByCropCd(wiCropYVo1.getCropCd());
            if (wiCropB != null) {
                wiCropYVo1.setCropNm(wiCropB.getCropNm());
            }
        }
        return wiCropYVo1;
    }

    /**
     * 查询年度种植作物列表
     *
     * @param wiCropY 年度种植作物
     * @return 年度种植作物
     */
    @Override
    public List<WiCropYVo> selectWiCropYList(WiCropY wiCropY) {
        List<WiCropYVo> wiCropYVos = wiCropYMapper.selectWiCropYList(wiCropY);
        for (WiCropYVo vo : wiCropYVos) {
            WiCropB wiCropB = wiCropBMapper.selectWiCropBByCropCd(vo.getCropCd());
            if (wiCropB != null) {
                vo.setCropNm(wiCropB.getCropNm());
            }
        }
        return wiCropYVos;
    }

    /**
     * 新增年度种植作物
     *
     * @param wiCropY 年度种植作物
     * @return 结果
     */
    @Override
    public int insertWiCropY(WiCropY wiCropY) {
        //先查询是否存在，若存在，直接返回失败
        WiCropYVo wiCropYVo = wiCropYMapper.selectWiCropYByCropYrAndCropCd(wiCropY);
        if (wiCropYVo != null) {
            return 0;
        }
        //查询当年年度作物种植代码 每年作物代码重新计算排列
        WiCropY wiCropYQuery = new WiCropY();
        wiCropYQuery.setCropYr(wiCropY.getCropYr());
        List<WiCropYVo> wiCropYS = wiCropYMapper.selectWiCropYList(wiCropYQuery);
        int plantCd;
        if (wiCropYS.size() == 0) {
            plantCd = 1;
        } else {
            plantCd = Integer.parseInt(wiCropYS.get(wiCropYS.size() - 1).getPlantCd()) + 1;
        }
        wiCropY.setPlantCd(ZeroFillUtils.zeroFill(plantCd, "00"));
        wiCropY.setDelFlag("0");
        return wiCropYMapper.insertWiCropY(wiCropY);
    }

    /**
     * 修改年度种植作物
     *
     * @param wiCropY 年度种植作物
     * @return 结果
     */
    @Override
    public int updateWiCropY(WiCropY wiCropY) {
        return wiCropYMapper.updateWiCropY(wiCropY);
    }

    /**
     * 批量删除年度种植作物
     *
     * @param ids 需要删除的年度种植作物主键
     * @return 结果
     */
    @Override
    public int deleteWiCropYByCropYrs(String[] ids) {
        return wiCropYMapper.deleteWiCropYByCropYrs(ids);
    }

    /**
     * 删除年度种植作物信息
     *
     * @param id 年度种植作物主键
     * @return 结果
     */
    @Override
    public int deleteWiCropYByCropYr(String id) {
        return wiCropYMapper.deleteWiCropYByCropYr(id);
    }
}
